# -*- coding: utf-8 -*-
# standard
import os
import sys
import re
import inspect
from datetime import datetime
from typing import List
import importlib

# third
from fastapi import FastAPI
from fastapi.routing import APIRoute

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)
from models.base_model import get_module_title
from toolbox.common_tools import logger


"""
@Title:   加载views
@File: __init__.py
@Author: walle 2024年11日28日 08时53分05秒
@Version: 1.0.0
@Desc: 
"""


def import_views(app: FastAPI | None = None):
    """
    导入views
    :param only: _description_
    """
    
    # 检出 views 文件夹下的所有文件
    b1 = datetime.now()
    views_path = os.path.dirname(__file__) 
    # 开始导入
    for file in os.listdir(views_path):
        if file.endswith(".py") and file != "__init__.py":
            module_name = file[:-3]
            b = datetime.now()
            module = importlib.import_module(f".{module_name}", package="views")
            router = getattr(module, "router", None)
            if router:
                app.include_router(router=router)
            e = datetime.now()
            module_title = get_module_title(module)
            logger.debug(f"加载 {module_title} 耗时: {(e - b).total_seconds()} 秒")
    e1 = datetime.now()
    logger.debug(f"导入视图完成! 导入总耗时: {(e1 - b1).total_seconds()} 秒")
        

if __name__ == '__main__':
    import_views()
    pass